package crochet.examples.helloworld;

/*
 Copyright 2011 Intuitive Solutions

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
import crochet.core.CrochetManager;
import crochet.core.IScheduler;
import crochet.core.ISchedulerDelegate;
import crochet.core.SchedulerAttribute;
import crochet.core.SchedulerType;
import crochet.core.configuration.CrochetConfiguration;

public class Program {
	protected int iTimes = 0, iTimesAsync = 0;

	public static void main(String[] args) {

		final Program pTestProgram = new Program();
		try {
			// Schedule process using Attributes.
			// CrochetManager.getInstance().dispose();
			/*
			 * CrochetManager.getInstance().schedule(new ISchedulerDelegate() {
			 * 
			 * @Override public void execute(IScheduler sSourceScheduler) throws
			 * Exception { pTestProgram.FailingMethod(sSourceScheduler); } });
			 */
			// CrochetManager.getInstance().dispose();

			// Schedule process without using Attributes.
			CrochetManager.getInstance().schedule(SchedulerType.ThreadPool,
					"Async10", new ISchedulerDelegate() {
						@Override
						public void execute(IScheduler sSourceScheduler) {
							pTestProgram.AsynchronousMethod(sSourceScheduler);
						}
					});
			CrochetManager.getInstance().schedule(SchedulerType.ThreadPool,
					"Async10", new ISchedulerDelegate() {
						@Override
						public void execute(IScheduler sSourceScheduler)
								throws Exception {
							pTestProgram
									.FailingAsynchronousMethod(sSourceScheduler);
						}
					});

			// Schedule process using Attributes.
			/*
			 * CrochetManager.getInstance().schedule(new ISchedulerDelegate() {
			 * 
			 * @Override public void execute(IScheduler sSourceScheduler) {
			 * pTestProgram.TimerMethod(sSourceScheduler); } });
			 */

			// CrochetManager.getInstance().dispose();
			// Schedule process using Attributes.
			/*
			 * CrochetManager.getInstance().schedule(new ISchedulerDelegate() {
			 * 
			 * @Override public void execute(IScheduler sSourceScheduler) {
			 * pTestProgram.SequentialMethod(sSourceScheduler); } });
			 */
			/*
			 * CrochetManager.getInstance().schedule(new ISchedulerDelegate() {
			 * 
			 * @Override public void execute(IScheduler sSourceScheduler) {
			 * pTestProgram.SequentialMethod(sSourceScheduler); } });
			 */
			/*
			 * CrochetManager.getInstance().schedule(new ISchedulerDelegate() {
			 * 
			 * @Override public void execute(IScheduler sSourceScheduler) {
			 * pTestProgram.SequentialMethod(sSourceScheduler); } });
			 */
			// CrochetManager.getInstance().dispose();
			System.out.println("6 - HelloWorld program.  Everything Ok.");

			/*
			 * CrochetManager.getInstance().schedule(SchedulerType.ThreadPool,
			 * "SubscriptionPool", new ISchedulerDelegate() {
			 * 
			 * @Override public void execute(IScheduler sSourceScheduler) {
			 * asynchronousMethod(sSourceScheduler); } });
			 */

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@SchedulerAttribute(schedulerType = SchedulerType.Immediate, name = "Retry")
	public void FailingMethod(IScheduler sSourceScheduler) throws Exception {
		System.out.println("1 - Failing Method.");

		if (iTimes++ < 2) {
			System.out.println("  Retrying...");
			throw new Exception();
		} else {
			System.out.println("  Done...");
		}
	}

	public void AsynchronousMethod(IScheduler sSourceScheduler) {
		for (int i = 0; i < 10; i++) {
			System.out.println("2 - Asynchronous Method.  Everything Ok.");

			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		CrochetManager.getInstance().dispose();
	}

	public void FailingAsynchronousMethod(IScheduler sSourceScheduler)
			throws Exception {
		System.out.println("3 - Failing Asynchronous Method.");

		if (iTimesAsync++ < 2) {
			System.out.println("  Retrying...");
			throw new Exception();
		} else {
			System.out.println("  Done...");
		}
	}

	/*
	 * @SchedulerAttribute(schedulerType = SchedulerType.TimerPool, name =
	 * "Timer") public void TimerMethod(IScheduler sSourceScheduler) {
	 * System.out.println("4 - Timer Method.  Everything Ok."); }
	 */

	@SchedulerAttribute(schedulerType = SchedulerType.Sequential, name = "Sequential")
	public void SequentialMethod(IScheduler sSourceScheduler) {
		System.out.println("5 - Sequential Method.  Everything Ok.  "
				+ (iTimes++));
	}
}